﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Admin.Common;
using Zocono.WCS.Application.AppTraces;
using Zocono.WCS.Domain.PlcBytesObject;

namespace Zocono.WCS.Admin.Controllers.MonitorManageMoudle
{
    public class MonitorFloor1Controller : BaseController
    {
        public MonitorFloor1Controller(IMemoryCache memoryCache, TracesApp tracesApp, IHttpContextAccessor httpContextAccessor) : base(memoryCache, tracesApp, httpContextAccessor)
        {
        }

        public IActionResult Index()
        {
            var signalRUrl = JsonConfigurationHelper.GetJson("appsettings.json", "signalRUrl");
            ViewBag.signalRUrl = signalRUrl;
            return View();
        }




        public async Task<JsonResult> OperatePlcBoAsync()
        {
            bool result = false;
            string msg = "失败！";
            var objectType = Request.Form["objectType"].ToString();
            if (objectType.Contains("Sc"))
            {

            }
            else
            {
                PlanarGeneralWriteWcsAction planarBo = new PlanarGeneralWriteWcsAction();
                planarBo.ObjectType = objectType;
                planarBo.TaskCode = Request.Form["TaskCode"].ToInt32();
                planarBo.ActionType = Request.Form["ActionType"].ToByte();
                planarBo.EquipmentCode = Request.Form["EquipmentCode"].ToInt32();
                planarBo.PalletBarcode = Request.Form["PalletBarcode"].ToString();
                planarBo.PalletType = Request.Form["PalletType"].ToByte();
                planarBo.ToEquipmentCode = Request.Form["ToEquipmentCode"].ToInt32();
                planarBo.AlrmCode = Request.Form["AlrmCode"].ToByte();
                planarBo.Reserve = Request.Form["Reserve"].ToByte();
                planarBo.Reserve2 = Request.Form["Reserve2"].ToByte();
                planarBo.Reserve3 = Request.Form["Reserve3"].ToByte();
                planarBo.Reserve4 = Request.Form["Reserve4"].ToByte();
                try
                {
                    HeaderResult<bool> headerResult = await SimpleApiClient.PostAsync<HeaderResult<bool>>(Parameters.OperatePlanarPlcBoAsync, planarBo);
                    //var res = await _plcWriteBoMange.WCSSetPlanerPLCAsync(planarBo);
                    result = headerResult.IsSucceed;
                    msg = headerResult.Message;
                }
                catch (Exception ex)
                {
                    result = false;
                    msg = ex.Message;
                }
            }

            return CreateJsonGetResult(result, msg);
        }


        public async Task<JsonResult> OperateWcsBoAsync()
        {
            bool result = false;
            string msg = "失败！";
            var objectType = Request.Form["objectType"].ToString();
            if (objectType.Contains("Sc"))
            {
                ZJNScMessageWriteWcsActionBO zjnScBo = new ZJNScMessageWriteWcsActionBO();
                zjnScBo.ObjectType = objectType;
                zjnScBo.Startcommand = Request.Form["Startcommand"].ToBool();
                zjnScBo.Alarmreset = Request.Form["Alarmreset"].ToBool();
                zjnScBo.WCSfirecommand = Request.Form["WCSfirecommand"].ToBool();
                zjnScBo.WCSPhotoFire = Request.Form["WCSPhotoFire"].ToBool();
                zjnScBo.WCSRgvFireHlper = Request.Form["WCSRgvFireHlper"].ToBool();
                zjnScBo.WCSDTSAlarm = Request.Form["WCSDTSAlarm"].ToBool();
                zjnScBo.TimeReset = Request.Form["TimeReset"].ToBool();
                zjnScBo.Taskreset = Request.Form["Taskreset"].ToBool();

                zjnScBo.Jobnumber = Request.Form["Jobnumber"].ToInt32();
                zjnScBo.Jobordercode = Request.Form["Jobordercode"].ToByte();
                zjnScBo.WCSpostingcompleted = Request.Form["WCSpostingcompleted"].ToByte();
                zjnScBo.StartlinefromPC = Request.Form["StartlinefromPC"].ToByte();
                zjnScBo.TargetlinefromPC = Request.Form["TargetlinefromPC"].ToByte();
                zjnScBo.StartcolumnfromPC = Request.Form["StartcolumnfromPC"].ToByte();
                zjnScBo.TargetcolumnfromPC = Request.Form["TargetcolumnfromPC"].ToByte();
                zjnScBo.StartlayerfromPC = Request.Form["StartlayerfromPC"].ToByte();
                zjnScBo.TargetlayerfromPC = Request.Form["TargetlayerfromPC"].ToByte();
                zjnScBo.yuliu8 = Request.Form["yuliu8"].ToByte();
                zjnScBo.yuliu9 = Request.Form["yuliu9"].ToByte();
                zjnScBo.yuliu10 = Request.Form["yuliu10"].ToByte();
                zjnScBo.yuliu11 = Request.Form["yuliu11"].ToByte();
                zjnScBo.yuliu12 = Request.Form["yuliu12"].ToByte();

                zjnScBo.EquipmentCode = Request.Form["EquipmentCode"].ToString().Substring(2).ToInt32();
                try
                {
                    HeaderResult<bool> headerResult = await SimpleApiClient.PostAsync<HeaderResult<bool>>(Parameters.OperateScWcsBoAsync, zjnScBo);
                    //var res = await _plcWriteBoMange.WCSSetSCPLCAsync(zjnScBo);
                    result = headerResult.IsSucceed;
                    msg = headerResult.Message;
                }
                catch (Exception e)
                {
                    result = false;
                    msg = e.Message;
                }
            }
            else
            {
                PlanarGeneralWriteWcsAction planarBo = new PlanarGeneralWriteWcsAction();
                planarBo.ObjectType = objectType;
                planarBo.TaskCode = Request.Form["TaskCode"].ToInt32();
                planarBo.ActionType = Request.Form["ActionType"].ToByte();
                planarBo.EquipmentCode = Request.Form["EquipmentCode"].ToInt32();
                planarBo.ToEquipmentCode = Request.Form["ToEquipmentCode"].ToInt32();
                planarBo.PalletBarcode = Request.Form["PalletBarcode"].ToString();
                planarBo.PalletType = Request.Form["PalletType"].ToByte();
                planarBo.AlrmCode = Request.Form["AlrmCode"].ToByte();
                planarBo.Reserve = Request.Form["Reserve"].ToByte();
                planarBo.Reserve2 = Request.Form["Reserve2"].ToByte();
                planarBo.Reserve3 = Request.Form["Reserve3"].ToByte();
                planarBo.Reserve4 = Request.Form["Reserve4"].ToByte();
                try
                {

                    HeaderResult<bool> headerResult = await SimpleApiClient.PostAsync<HeaderResult<bool>>(Parameters.OperatePlanarWcsBoAsync, planarBo);
                    //var res = await _plcWriteBoMange.WCSSetSCPLCAsync(zjnScBo);  
                    result = headerResult.IsSucceed;
                    msg = headerResult.Message;
                }
                catch (Exception ex)
                {
                    result = false;
                    msg = ex.Message;
                }
            }


            return CreateJsonGetResult(result, msg);
        }

    }
}
